Ext.define('IAG.controller.LogsController', {
    extend: 'Ext.app.Controller',
    stores: ['LogsStore'],

    models: ['Log'],

    views: ['logs.LogsView'],
    init: function() {
    	console.log("Starting logs controller...");
    	//ugly hack
    	myLogsController		= this;
    	
    	this.control({
//            'logs button[action=stopImport]' : {
//            	click: this.stopImport
//            },
//            'logs button[action=continueImport]' : {
//            	click: this.showAddSetting
//            },
//            'logs button[action=reloadFailedImport]' : {
//            	click: this.reload
//            }
    		 'logs button' : {
             	click: this.btnEventManager
             }
    	});
    },
    
    
    btnEventManager: function (button) {
    	var act		= button.action;
    	this[act]();
    },
    stopImport: function (button) {
    	Ext.Ajax.request({
    		method: "GET",
    		url: iag_constants.importfeeds_stop_import_url,
    		scope: this,
    		timeout: 120000,
    		currentBtn: button,
    		headers : {
    			'Content-Type' : 'application/json'
    		},
    		callback: function (options, success, response) {
//    			debugger;
    			if ( success ) {
    				Ext.Msg.alert("Stop Import", "Import stopped");
    			}
    			else {
    				Ext.Msg.alert("Stop Import", "Error from server when trying to stopo the import");
    			}
    		}
    	});
    	
   		return;
    },
    getLogsGrid: function () {
    	if ( this.logsGrid == null ) {

    		this.logsGrid	= Ext.create('IAG.view.logs.LogsView');
    		this.getStore("LogsStore").load();
    		
    	}
    	return this.logsGrid;
    },
    checkFeeds: function (button, url) {
    	var myUrl;
    	if ( url == null || typeof(url) != 'string' ) {
    		myUrl	= iag_constants.importfeeds_check_status_url;
    	}
    	else
    		myUrl	= url;
    	
    	button.toggle(true,true);
    	Ext.Ajax.request({
    		method: "GET",
    		url: myUrl,
    		scope: this,
    		timeout: 120000,
    		currentBtn: button,
    		headers : {
    			'Content-Type' : 'application/json'
    		},
    		callback: function (options, success, response) {
//    			debugger;
    			if ( success ) {
    				var ilStatus	= Ext.decode(response.responseText);
    				ilStatus			= ilStatus.ImportLogStatus;
    				var ret				= this.updateButtonText(options.currentBtn, ilStatus);
    				var controller	=  this;
    				this.getStore('LogsStore').load();
    				if ( ret ) {
    					var closure	= function () {controller.checkFeeds(options.currentBtn, iag_constants.importfeeds_check_status_url+"/"+ilStatus.currentTagNo)};
    					setTimeout(closure, iag_constants.pollingInterval );
    				}
    				else {
    					options.currentBtn.setText( "Reinitialize db" );
    					button.setIconCls('reloadAllMainToolbarButton');
    					options.currentBtn.toggle(false, true);
    				}
    			}
    		}
    	});
    },
    updateButtonText: function(button, ilStatus) {
		if ( ilStatus != null && ilStatus.finished != null && ! ilStatus.finished ) {
			var statusStr	= "Processing " + ilStatus.currentTagNo + "/" + ilStatus.totalNoTags + "..." ;
			button.setText( statusStr );
			button.setIconCls('loadingIcon');
			return true;
		}
		return false;
    } ,
    reloadFeed: function(logId) {
    	var buttonEl	= Ext.ComponentQuery.query('.userlist button[action=reloadAllFeeds]')[0];
    	this.checkFeeds (buttonEl, iag_constants.importfeeds_load_url + "/" + logId);
    },
    reloadFailedImport: function() {
    	//debugger;
    	var buttonEl	= Ext.ComponentQuery.query('.userlist button[action=reloadAllFeeds]')[0];
    	this.checkFeeds (buttonEl, iag_constants.importfeeds_load_url);
    },
    reloadLogs: function() {
    	this.getStore("LogsStore").load();
    }

});